package com.mmy.controller.oa;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.oa.LeaveInfo;
import com.mmy.domain.oa.LeaveType;
import com.mmy.domain.oa.VSysLeaveInfo;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.oa.impl.LeaveInfoServiceImpl;
import com.mmy.service.oa.impl.LeaveTypeServiceImpl;

/**
 * 类的介绍：请假记录
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class LeaveInfoController {

	@Autowired
	LeaveInfoServiceImpl leaveInfoServiceImpl;
	@Autowired
	LeaveTypeServiceImpl leaveTypeServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-04
	 */
	@RequestMapping(value = "/leaveinfo")
	public String ToLeaveInfo(Model model) {
		try {

			List<VSysLeaveInfo> lstLeaveInfo = leaveInfoServiceImpl.findAll();
			model.addAttribute("llist", lstLeaveInfo);
			List<LeaveType> lstLeaveType = leaveTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstLeaveType);
			return "OA/leaveandreview/leaveinfo";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createleaveinfo")
	public String ToCreateLeaveInfo(Model model) {
		List<LeaveType> lstLeaveType = leaveTypeServiceImpl.findAll();
		model.addAttribute("ltlist", lstLeaveType);
		return "OA/leaveandreview/createleaveinfo";
	}

	/**
	 * 函数功能说明： 新建请假记录
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-04
	 */
	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");

	@RequestMapping(value = "/subcreateleaveinfo")
	public String CreateLeaveInfo(LeaveInfo leaveInfo, String startTime, String endTime, HttpSession session)
			throws Exception {

		String strStart = new String(startTime.replace("T", " "));
		leaveInfo.setStart(simpleDateFormat.parse(strStart));

		String strEnd = new String(endTime.replace("T", " "));
		leaveInfo.setEnd(simpleDateFormat.parse(strEnd));

		long lDay = simpleDateFormat.parse(strEnd).getTime() - simpleDateFormat.parse(strStart).getTime();
// 天
		byte iDay = (byte) (lDay / (1000 * 60 * 60 * 24));
//		时分
//		long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
//		 
//		long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60))
//				/ (1000 * 60);

		if (iDay < 0) {
			return "test";
		}
		leaveInfo.setDay(iDay);

		try {

			// 获取最大值
			Object oLeaveID = leaveInfoServiceImpl.getMaxLeaveID();
			/* 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 */
			if (oLeaveID == null) {
				leaveInfo.setLeaveID(1);

			} else {
				leaveInfo.setLeaveID((int) oLeaveID + 1);
			}

			leaveInfo.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			leaveInfo.setCreator(tSysUser.getUserName());
			leaveInfoServiceImpl.save(leaveInfo.getLeaveID());
			leaveInfoServiceImpl.save(leaveInfo);

			return "redirect:leaveinfo";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除请假记录
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-04
	 */

	@PostMapping("/delleaveinfo")
	@ResponseBody
	public String DelLeaveInfo(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstLeaveID = new ArrayList<>();

		for (String str : checklist) {
			lstLeaveID.add(Integer.parseInt(str));
		}

		leaveInfoServiceImpl.delLeaveInfo(lstLeaveID);

		return "OA/leaveandreview/leaveinfo";
	}

	/**
	 * 函数功能说明： 修改请假记录
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-04
	 */

	@RequestMapping(value = "/enterupdateleaveinfo")
	public String EnterUpdateLeaveInfo(HttpServletRequest request, Model model) {

		int iLeaveID = Integer.parseInt(request.getParameter("iLeaveID"));

		LeaveInfo leaveInfo = leaveInfoServiceImpl.findByLeaveID(iLeaveID);
		model.addAttribute("leaveInfo", leaveInfo);
		List<LeaveType> lstLeaveType = leaveTypeServiceImpl.findAll();
		model.addAttribute("ltlist", lstLeaveType);
		return "OA/leaveandreview/updateleaveinfo";

	}

	@RequestMapping(value = "/updateleaveinfo")
	public String UpdateLeaveInfo(LeaveInfo leaveInfo, HttpSession session) throws Exception {

		try {

			/* 定义一个新的实体类对象 */
			LeaveInfo dleaveInfo = leaveInfoServiceImpl.findByLeaveID(leaveInfo.getLeaveID());
			dleaveInfo.setTypeID(leaveInfo.getTypeID());
			dleaveInfo.setReason(leaveInfo.getReason());
			if (dleaveInfo.getState() == 4) {
				dleaveInfo.setState((byte) 2);
			}
			if (dleaveInfo.getFlag() == 1) {
				dleaveInfo.setFlag((byte) 1);
			}
			dleaveInfo.setMemo(leaveInfo.getMemo());
			dleaveInfo.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dleaveInfo.setReviser(tSysUser.getUserName());
			leaveInfoServiceImpl.save(dleaveInfo);
			return "redirect:leaveinfo";

		} catch (Exception e) {

		}
		return null;
	}

	@RequestMapping(value = "/entercheckupdateleaveinfo")
	public String EnterCheckUpdateLeaveInfo(HttpServletRequest request, Model model) {

		int iLeaveID = Integer.parseInt(request.getParameter("iLeaveID"));

		LeaveInfo leaveInfo = leaveInfoServiceImpl.findByLeaveID(iLeaveID);
		model.addAttribute("leaveInfo", leaveInfo);
		String strTypeName = leaveTypeServiceImpl.findByTypeID(leaveInfo.getTypeID()).getTypeName();
		model.addAttribute("strTypeName", strTypeName);
		return "OA/leaveandreview/checkupdateleaveinfo";

	}

	@RequestMapping(value = "/checkupdateleaveinfo")
	public String CheckUpdateLeaveInfo(HttpServletRequest request, LeaveInfo leaveInfo) throws Exception {

		int iFlag = Integer.parseInt(request.getParameter("iFlag"));
		try {

			/* 定义一个新的实体类对象 */
			LeaveInfo dleaveInfo = leaveInfoServiceImpl.findByLeaveID(leaveInfo.getLeaveID());
			dleaveInfo.setTypeID(leaveInfo.getTypeID());
			dleaveInfo.setReason(leaveInfo.getReason());
			dleaveInfo.setMemo(leaveInfo.getMemo());
			if (iFlag == 3) {
				dleaveInfo.setState((byte) 3);
				dleaveInfo.setFlag((byte) 3);
			}
			if (iFlag == 1 && dleaveInfo.getFlag() == 0) {
				dleaveInfo.setState((byte) 4);
				dleaveInfo.setFlag((byte) 1);
			} else if (iFlag == 1 && dleaveInfo.getFlag() == 1) {
				dleaveInfo.setState((byte) 1);
				dleaveInfo.setFlag((byte) 2);
			}
			dleaveInfo.setMessage(leaveInfo.getMessage());
			dleaveInfo.setConfirm(new Date());
			leaveInfoServiceImpl.save(dleaveInfo);
			return "redirect:leaveinfo";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询请假记录信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-05
	 */
	@RequestMapping(value = "/searchleaveinfo")
	public String FindLeaveInfo(HttpServletRequest request, Model model) {

		String strTypeName = request.getParameter("TypeName");
		if (strTypeName == "") {
			List<VSysLeaveInfo> lstleaveInfo = leaveInfoServiceImpl.findAll();
			model.addAttribute("llist", lstleaveInfo);
			List<LeaveType> lstLeaveType = leaveTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstLeaveType);
			return "OA/leaveandreview/leaveinfo";
		} else {
			List<VSysLeaveInfo> lstleaveInfo = leaveInfoServiceImpl.findByTypeName(strTypeName);
			model.addAttribute("llist", lstleaveInfo);
			List<LeaveType> lstLeaveType = leaveTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstLeaveType);
			return "OA/leaveandreview/leaveinfo";
		}

	}
}
